package com.me.exam.controller;

import com.me.exam.model.Stu;
import com.me.exam.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("stu")
public class StuController {
	@Autowired
	private StuService stuService;
	
	@GetMapping
	public String index(Model model){
		model.addAttribute("stus", stuService.findAll());
		return "stu/index";
	}
	
	@GetMapping("/add")
	public String add(){
		return "stu/add";
	}
	
	@PostMapping("/add")
	public String addStu(String name,String major){
		stuService.save(Stu.builder().name(name).major(major).build());
		return "redirect:/stu";
	}
}
